<template>
  <el-container>
    <el-main>
      <div style="display: flex; flex-wrap: wrap; justify-content: center; margin-bottom: 20px">
        <div class=wrap v-for="(value, key) in bedMap" :key="key">
          <RoomStatus :title="key" :bedList="value"  @refresh="getList"/>
        </div>
      </div>
      <div style="display: flex; flex-wrap: wrap; justify-content: center; margin-bottom: 20px">
        <el-button @click="startBiz" > 手动开诊 </el-button>
        <el-button @click="closeBiz" > 手动闭诊 </el-button>
        <el-button @click="resetRoom" > 重置诊室 </el-button>
        <el-button @click="resetSchedule" > 重置定时任务 </el-button>
        <el-button @click="refresh" > 刷新 </el-button>
      </div>
    </el-main>
    <el-aside width="250px">
      <div>开诊时间：{{ openingPeriod }}  {{ monitorInfo.openingFlag }}  </div>
      <div>工位概况： {{monitorInfo.queueNum}} {{monitorInfo.activeQueueNum}} {{monitorInfo.priorityQueueNum}} </div>
      <div v-for="(value, key) in monitorInfo.checkTypeBedInfo" :key="key">
        {{ checkTypeStore.getCheckTypeName(Number(key)) }}  {{ value }}
      </div>
    </el-aside>
  </el-container>
</template>

<script setup lang="ts">
import {RoomStatus} from "@/components/RoomStatus"
import { RoomApi, RoomVO, MonitorInfo } from '@/api/ecg/room'
import { QueueApi } from '@/api/ecg/queue'
import { getConfigKey } from '@/api/infra/config'
import {useCheckTypeStore} from "@/store/modules/checkType";

defineOptions({ name: 'RoomSetting' })

const bedMap = ref() // 列表的数据

const openingPeriod = ref<string>('')
const monitorInfo = ref<MonitorInfo>({
  queueNum: 0,
  activeQueueNum: 0,
  priorityQueueNum: 0,
  openingFlag: 0,
  checkTypeBedInfo: {}
})

const checkTypeStore = useCheckTypeStore();

const getOpeningPeriod = async () => {
  const data = await getConfigKey('ecg.openingtime')
  openingPeriod.value = data
}

const getMonitorInfo = async () => {
  const data = await RoomApi.getMonitorInfo()
  monitorInfo.value = data as MonitorInfo
}

/** 查询列表 */
const getList = async () => {
    const data = await RoomApi.getAllBedMap()
    bedMap.value = data as Map<String, RoomVO[]>
}

const startBiz = async () => {
  await RoomApi.startBiz();
  getMonitorInfo()
}

const closeBiz = async () => {
  await RoomApi.closeBiz();
  getMonitorInfo()
}

const resetRoom = async () => {
    await RoomApi.resetRoom();
    getList()
}

const resetSchedule = async () => {
    await QueueApi.resetSchedule();
}

const refresh = () => {
  getList()
  getOpeningPeriod()
  getMonitorInfo()
}

/** 初始化 **/
onMounted(() => {
  getList()
  getOpeningPeriod()
  getMonitorInfo()
})

</script>

<style scoped lang="scss">
.wrap {
  margin-right: 20px;
}
</style>
